package com.jeeplus.test.jlnu.graduationInfo.util;


import com.jeeplus.test.jlnu.graduationInfo.domain.entity.CustomMultipartFile;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.errors.MinioException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.stereotype.Component;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


@Component
public class PdfSignUtil {

    public static MultipartFile signPdf(MultipartFile file) throws IOException {
        PDDocument document = PDDocument.load(file.getInputStream());
        PDPage page = document.getPage(0);
        PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true);

        // 生成红色圆形签章
        float width = page.getMediaBox().getWidth();
        float height = page.getMediaBox().getHeight();
        float radius = 50; // 签章半径

        contentStream.setStrokingColor(Color.RED);
        contentStream.setLineWidth(2);
        contentStream.addRect((width - radius * 2) / 2, 40, radius * 2, radius * 2);
        contentStream.stroke();

        // 加载支持中文的字体（使用系统字体或者你提供的 TTF 字体）
        Resource fontResource = new ClassPathResource("fonts/simhei.ttf"); // 字体文件路径
        try (InputStream fontStream = fontResource.getInputStream()) {
            PDType0Font font = PDType0Font.load(document, fontStream);

            contentStream.beginText();
            contentStream.setFont(font, 12);
            contentStream.setNonStrokingColor(Color.RED);
            contentStream.newLineAtOffset((width - 30) / 2, 60);
            contentStream.showText("电子印章");  // 中文文本
            contentStream.endText();
        }
        contentStream.close();

        document.close();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        document.save(byteArrayOutputStream);
        document.close();

        byte[] signedPdfBytes = byteArrayOutputStream.toByteArray();

        // 返回签名后的文件作为 MultipartFile
        return new CustomMultipartFile("signed_certificate", "signed_certificate.pdf", "application/pdf", signedPdfBytes);
        }

        // 下载文件
    public byte[] downloadFileFromMinio(String filePath) throws MinioException, IOException, NoSuchAlgorithmException, InvalidKeyException {
        // 初始化 Minio 客户端
        MinioClient minioClient = MinioClient.builder()
                .endpoint("https://your-minio-endpoint")
                .credentials("access-key", "secret-key")
                .build();

        // 下载文件
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        InputStream inputStream = minioClient.getObject(
                GetObjectArgs.builder()
                        .bucket("your-bucket-name")
                        .object(filePath)
                        .build()
        );

        // 读取文件内容到字节数组
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            byteArrayOutputStream.write(buffer, 0, length);
        }

        byte[] fileBytes = byteArrayOutputStream.toByteArray();

        inputStream.close();
        byteArrayOutputStream.close();

        return fileBytes;
    }
}
